package com.yitong.equipment.utils

import android.content.res.Configuration
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.SDCardUtils
import com.yitong.equipment.App
import java.io.File

/**
 * @desc 缓存工具
 */
object CacheUtils {
    fun createOrExistsDir(file: File): File {
        FileUtils.createOrExistsDir(file)
        return file
    }

    /**
     * 获取文件保存目录(需要读写权限)
     *
     * @return /storage/emulated/0/yitong/{ProjectName}/cache
     */
    private val rootSaveFile: File
        private get() {
            var path: String? = ""
            if (SDCardUtils.isSDCardEnableByEnvironment()) {
                path = SDCardUtils.getSDCardPathByEnvironment()
            } else if (!SDCardUtils.getSDCardInfo().isEmpty()) {
                path = SDCardUtils.getSDCardInfo()[0].path
            }
            return File(
                path,
                "yitong" + File.separator + "yt_equipment" + File.separator + "cache"
            )
        }
    val imageSaveFile: File
        get() = createOrExistsDir(File(rootSaveFile, "image"))
    val videoSaveFile: File
        get() = createOrExistsDir(File(rootSaveFile, "video"))
    val mediaSaveFile: File
        get() = createOrExistsDir(File(rootSaveFile, "media"))

    /**
     * 获取缓存文件目录(不需要权限)
     *
     * @return /storage/emulated/0/Android/data/{packageName}/cache
     */
    private val rootCacheFile: File?
        private get() = App.instance.externalCacheDir
    val compressCacheFile: File
        get() = createOrExistsDir(File(rootCacheFile, "compress"))
    val httpCacheFile: File
        get() = createOrExistsDir(File(rootCacheFile, "http"))
    val dataCacheFile: File
        get() = createOrExistsDir(File(rootCacheFile, "data"))
}